#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;

#endregion

namespace TVC.Forms
{
    public partial class frmResetPassword : Form
    {
        string sErr = "";
        public frmResetPassword()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            BUS.PODControl ctr = new BUS.PODControl();
            DTO.PODInfo inf = ctr.Get(user.Text, ref sErr);
            if (inf.USER_ID == "")
                lbErr.Text = "User name or Email is not correct!";
            else
            {
                //BUS.TVCEmployeesControl empCtr = new BUS.TVCEmployeesControl();
                //DTO.TVCEmployeesInfo empInf = empCtr.Get("TVC", inf.EMP_ID, ref sErr);
                //if (empInf.EmpID != "")
                //{
                //    if (empInf.Email != "")
                //    {
                //        if (empInf.Email != email.Text)
                //            lbErr.Text = "User name or Email is not correct!";
                //        else
                //        {
                //            ResetPwd(inf);
                //            lbErr.Text = "Your new password sent to your mail!";
                //        }
                //    }
                //    else
                //    {
                //        empInf.Email = email.Text;
                //        empCtr.Update(empInf);
                //        ResetPwd(inf);
                //        lbErr.Text = "Your new password sent to your mail!";
                //    }
                //}
                //else
                //{
                //    lbErr.Text = "User name is not exist in this system!";
                //}
            }
        }

        private void ResetPwd(DTO.PODInfo inf)
        {
            BUS.PODControl ctr = new BUS.PODControl();
            string randomstring = RandomString(6);
            inf.PASS = Convert.ToBase64String(new System.Security.Cryptography.SHA1CryptoServiceProvider().ComputeHash(Encoding.ASCII.GetBytes(randomstring)));

            ctr.Update(inf);
            Sendmail sm = new Sendmail(Properties.Settings.Default.Email, Properties.Settings.Default.Password, "gmail.com", "smtp", 587);
            sm.SendMail("Sales Force Automation - Reset Password", "New Password: " + randomstring, email.Text, inf.USER_NAME, "", true, true);

        }
        private readonly Random _rng = new Random();
        private const string _chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";

        private string RandomString(int size)
        {
            char[] buffer = new char[size];

            for (int i = 0; i < size; i++)
            {
                buffer[i] = _chars[_rng.Next(_chars.Length)];
            }
            return new string(buffer);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}